﻿using _1._11._2015_BookReview.Data;
using _1._11._2015_BookReview.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity.Migrations;
using _1._11._2015_BookReview.Data.Models;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity;

namespace _1._11._2015_BookReview.Adapter
{
    public class BookReviewDataAdapter : IBookReviewAdapter
    {
        public List<BookViewModel> GetBooksViewModel()
        {
            List<BookViewModel> books = null;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                books = db.Books.Select(b => new BookViewModel
                {
                    AuthorFName = b.AuthorFName,
                    AuthorLName = b.AuthorLName,
                    Title = b.Title,
                    BookId = b.BookId,
                    Reviews = db.Reviews.Where(r => r.BookId == b.BookId).Select(r => new ReviewViewModel
                    {
                        Text = r.Text,
                        Reviewer = new ReviewerViewModel
                        {
                            ReviewerId = r.User.Id,
                            ReviewerName = r.User.FirstName + " " + r.User.LastName
                        }


                    }).ToList()
                }).ToList();
            }
            return books;
        }

        public BookViewModel GetBookViewModel(int id)
        {
            BookViewModel bookModel = new BookViewModel();
            Book book = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                book = db.Books.FirstOrDefault(b => b.BookId == id);
                {
                    bookModel.AuthorFName = book.AuthorFName;
                    bookModel.AuthorLName = book.AuthorLName;
                    bookModel.Title = book.Title;
                    bookModel.BookId = book.BookId;


                    bookModel.Reviews = db.Reviews.Where(r => r.BookId == id).Select(r => new ReviewViewModel
                    {
                        Text = r.Text,
                        Reviewer = new ReviewerViewModel
                        {
                            ReviewerId = r.User.Id,
                            ReviewerName = r.User.FirstName + " " + r.User.LastName
                        }


                    }).ToList();
                }
            }

            return bookModel;
        }


        public void AddBook(BookViewModel model)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                db.Books.Add(new Book
                {
                    Title = model.Title,
                    AuthorFName = model.AuthorFName,
                    AuthorLName = model.AuthorLName
                });
                db.SaveChanges();
            }
        }

        public ReviewViewModel GetBlankReview(int bookId)
        {
            Book book = null;
            //BookViewModel bookModel = new BookViewModel();
            ReviewViewModel review = new ReviewViewModel();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                book = db.Books.FirstOrDefault(b => b.BookId == bookId);
            }

            //bookModel.BookId = book.BookId;
            //bookModel.Title = book.Title;
            //bookModel.AuthorFName = book.AuthorFName;
            //bookModel.AuthorLName = book.AuthorLName;
            review.Title = book.Title;
            review.BookId = bookId;
            return review;
        }

        public void AddReview(ReviewViewModel review, string userId, int bookId)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {

                db.Reviews.Add(new Review
                {
                    BookId = bookId,
                    Text = review.Text,
                    UserId = userId
                });
                db.SaveChanges();
            }

        }

    }
}